<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="../../../css/mui.min.css" />
	<link rel="stylesheet" type="text/css" href="../../../css/pages/issue.css" />
</head>

<body>
	<div id="app" class="content">
		<div class="header-btn">
			<span :class="userData.carType==1?'vehicle':'person'" @click="userData.carType=1">我是车主</span>
			<span :class="userData.carType==2?'vehicle':'person'"  @click="userData.carType=2">我是乘客</span>
		</div>
		<div class="list m-t-20">
			<div class="item">
				<img class="icon" src="../images/location.png">
				<span>联系人</span>
				<input class="car-input" v-model="userData.carContact" type="text" placeholder="你的名字" />
			</div>
			<div class="item">
				<img class="icon" src="../images/location.png">
				<span>联系电话</span>
				<input class="car-input" v-model="userData.carPhone" type="number" placeholder="输入联系电话" />
			</div>
			<div class="item">
				<img class="icon" src="../images/location.png">
				<span>始发地</span>
				<input class="car-input" v-model="userData.carStartLocation" type="text" placeholder="输入始发地" />
			</div>
			<div class="item">
				<img class="icon" src="../images/location.png">
				<span>目的地</span>
				<input class="car-input" type="text" v-model="userData.carEndLocation" placeholder="输入目的地" />
			</div>
			<div class="item"  @click="carSetoffTime">
				<img class="icon" src="../images/time.png">
				<span class="font-c-min" v-if="userData.carSetoffTime" v-text="userData.carSetoffTime"></span>
				<span v-else>出发时间</span>
				<span class="mui-icon mui-icon-forward"></span>
			</div>
		</div>
		<div  v-show="userData.carType==1">
			<div class="title">车辆信息</div>
			<div v-if="carData.isVehicle==0" class="m-t-10 font-14 font-c-7f">暂无车辆信息，请前去我的主页添加</div>
			<div v-else class="font-16 font-c-14 m-t-10">
				<span v-text="carData.vehmName"></span>
				<span v-text="carData.vehmColour"></span>
				<span v-text="carData.vehmNumberPlate"></span>
			</div>
		</div>
		
		<div class="details m-t-25">
			<span class="title m-b-10">备注</span>
			<textarea rows="5" cols="2" v-model="userData.carRemarks"></textarea>
		</div>
		<div class="botton-wapper" @click="issueTap">发布</div>
	</div>
	<script src="../../../js/Width.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/vue.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/Util.js" type="text/javascript" charset="utf-8"></script>
	<script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
	<script type="text/javascript">
		mui.init()
		mui.plusReady(function () {
			if (!mui.os.android) {
				FastClick.attach(document.body);
			}
			GetVehicle()
		})

		function GetVehicle() {
			mui.post(Util.HTTP + 'api/Carpool/GetVehicle', {
				userID: Util.getState('$state').userID
			}, function (res) {
				if (res.code == 1001) {
					app.userData.carContact = res.data.carContact;
					app.userData.carPhone = res.data.carPhone;
					app.carData = res.data;
				}
			})
		}

		var app = new Vue({
			el: "#app",
			data: {
				userData: {
					carContact: '',
					carPhone: '',
					carType: 1,
					carStartLocation: '',
					carEndLocation: '',
					carSetoffTime: '',
					carRemarks: ''
				},
				carData: {}
			},
			methods: {
				issueTap: function () { // 发布方法,,
					var userData = this.userData
					if(userData.carType==1 && this.carData.isVehicle==0){
						mui.toast('暂无车辆信息，请前去我的主页添加')
						return
					}
					if (this.verification(userData)) {
						plus.nativeUI.showWaiting('提交中', {
							width: '100px',
							height: '100px'
						});
						this.PostCarpool(userData)
					}
				},
				PostCarpool: function (data) {
					var carData = this.carData;
					var vehmID = data.carType == 2 ? "" : carData.vehmID
					mui.post(Util.HTTP + 'api/Carpool/PostCarpool',{
						neiID: Util.getState('neiID').neiID,
						userID: Util.getState('$state').userID,
						vehmID: vehmID,
						carType: data.carType,
						carContact: data.carContact,
						carPhone: data.carPhone,
						carStartLocation: data.carStartLocation,
						carEndLocation: data.carEndLocation,
						carSetoffTime: data.carSetoffTime,
						carRemarks: data.carRemarks
					},function(res){
						plus.nativeUI.closeWaiting();
						if (res.code == 1001) {
							mui.toast('提交成功')
							setTimeout(function () {
								mui.back()
								var list = plus.webview.currentWebview().opener();
								//触发父页面的自定义事件(refresh),从而进行刷新
								mui.fire(list, 'issueBack');
								//返回true,继续页面关闭逻辑
								return true;
							}, 1000)
						}else {
							mui.toast(res.message)
						}
					})
				},
				verification: function (data) { // 字符串遍历 判断是否都填写完整,
					var state;
					Object.keys(data).forEach(function (key) {
						console.log(key + ':' + data[key])
						if (!data[key]) {
							mui.toast("请填写完整")
							state = false
						} else {
							state = true
						}
					});
					return state;
				},
				carSetoffTime: function () {
					var that = this;
					var presentDate = Util.getNowDate().time
					plus.nativeUI.pickDate(function (e) {
						var d = e.date;
						plus.nativeUI.pickTime(function (e) {
							var w = e.date;
							var Minutes = w.getMinutes() > 10 ? w.getMinutes() : '0' + w.getMinutes()
							console.log("选择的时间：" + w.getHours() + ":" + w.getMinutes());
							var carSetoffTime = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + w.getHours() +
								":" + Minutes + ":" + "00";
							if (compareDate(carSetoffTime, presentDate)) {
								that.userData.carSetoffTime = carSetoffTime
							} else {
								mui.toast('不能小于当前时间')
							}
						});
					});
				},
				
			},
		})
		
		function compareDate(s1, s2) {
			return ((new Date(s1.replace(/-/g, "\/"))) > (new Date(s2.replace(/-/g, "\/"))));
		}
	</script>
</body>

</html>
